package com.example.user.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 有序事件监听器
 * 可通过@Order指定顺序
 * @Async 异步执行，此时无序
 * @author vanson
 */
@Slf4j
@Component
@Order(1)
public class UserEmailNotifyListener implements ApplicationListener<UserNotifyEvent> {

    @Async("userNotifyPool")
    @Override
    public void onApplicationEvent(UserNotifyEvent userNotifyEvent) {
        log.info("第一步：发送邮件，内容：{}", userNotifyEvent.getUser().toString());
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            log.error("线程异常，记录到db，进行人工重发", e);
        }
        log.info("邮件已发送");
    }
}
